/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author Slave Jovanovski (slave@google.com)
 * @author Marc Hemeon (hemeon@google.com)
 *
 */

/**
 * Default constructor.
 */
YouTubeWatch = function() {
};


/**
 * @returns {Element} The player DOM element.
 */
YouTubeWatch.getPlayer = function() {
  return document.getElementById('movie_player');
};


/**
 * @returns {String} The title of the video playing on the watch page.
 */
YouTubeWatch.getVideoTitle = function() {
  return document.getElementById('eow-title');
};


// Set a listener for requests from the main extension. Send back  data from the
// watch page.
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (request.type == 'get_info') {
    var player = YouTubeWatch.getPlayer();
    var title = YouTubeWatch.getVideoTitle();
    var status = player.getPlayerState();
    if (player) {
      var message = {
        id: request.id,
        title: title.title,
        status: status
      };
      sendResponse(message);
    }
  } else if (request.type == 'pause_video') {
    var player = YouTubeWatch.getPlayer();
    if (player) {
      player.pauseVideo();
    }
    sendResponse();
  } else if (request.type == 'play_video') {
    var player = YouTubeWatch.getPlayer();
    if (player) {
      player.playVideo();
      sendResponse();
    }
  }
});
